package com.ease.business.systemmonitor.job;

import java.util.ArrayList;
import java.util.List;

import com.ease.access.systemmonitor.service.AlarmConfAccessService;
import com.ease.access.systemmonitor.service.AlarmInfoAccessService;
import com.ease.access.systemmonitor.service.DbTableSpaceAccessService;
import com.ease.access.systemmonitor.service.UnixServerAccessService;
import com.ease.model.systemmonitor.AlarmConf;
import com.ease.model.systemmonitor.AlarmInfo;
import com.ease.model.systemmonitor.DbTableSpace;
import com.ease.model.systemmonitor.UnixServer;

public class GetAlarmInfoTask {
	private AlarmConfAccessService alarmConfAccessService;

	private AlarmInfoAccessService alarmInfoAccessService;

	private DbTableSpaceAccessService dbTableSpaceAccessService;

	private UnixServerAccessService unixServerAccessService;

	public AlarmConfAccessService getAlarmConfAccessService() {
		return alarmConfAccessService;
	}

	public void setAlarmConfAccessService(AlarmConfAccessService alarmConfAccessService) {
		this.alarmConfAccessService = alarmConfAccessService;
	}

	public AlarmInfoAccessService getAlarmInfoAccessService() {
		return alarmInfoAccessService;
	}

	public void setAlarmInfoAccessService(AlarmInfoAccessService alarmInfoAccessService) {
		this.alarmInfoAccessService = alarmInfoAccessService;
	}

	public DbTableSpaceAccessService getDbTableSpaceAccessService() {
		return dbTableSpaceAccessService;
	}

	public void setDbTableSpaceAccessService(DbTableSpaceAccessService dbTableSpaceAccessService) {
		this.dbTableSpaceAccessService = dbTableSpaceAccessService;
	}

	public UnixServerAccessService getUnixServerAccessService() {
		return unixServerAccessService;
	}

	public void setUnixServerAccessService(UnixServerAccessService unixServerAccessService) {
		this.unixServerAccessService = unixServerAccessService;
	}

	public void execute() {
		// TODO Auto-generated method stub
		try {
			generateAlarmInfo();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void generateAlarmInfo() {
		List<AlarmConf> alarmConfList = new ArrayList<AlarmConf>();
		alarmConfList = alarmConfAccessService.findAll(null);
		if (alarmConfList != null)
			for (int i = 0; i < alarmConfList.size(); i++) {
				AlarmConf bean = alarmConfList.get(i);
				if (bean.getAlarmType().equals("0")) { // CPU
					List<UnixServer> listUnixServer = unixServerAccessService.findUnixServerInfoByPcid(bean.getPcid());
					UnixServer unixServer = listUnixServer.get(0);
					String[] cpuUsage = unixServer.getCpuUsage().split("%");
					String cpuOver = bean.getAlramThreshold();
					if (Float.parseFloat(cpuUsage[0]) > Float.parseFloat(cpuOver)) {
						AlarmInfo model = new AlarmInfo();
						model.setAlarmLevel(bean.getAlarmLevel());
						model.setAlarmType(bean.getAlarmType());
						model.setAlarmReason("CPU");
						model.setAlarmSend("系统自动告警");
						model.setAlarmType(bean.getAlarmType());
						model.setMemo("IP：【" + unixServer.getIp() + "】机器的" + "CPU使用率【" + cpuUsage[0] + "%】超过阀值【" + bean.getAlramThreshold() + "%】");
						alarmInfoAccessService.save(model);
					} else
						continue;
				} else if (bean.getAlarmType().equals("1")) {// 内存
					List<UnixServer> listUnixServer = unixServerAccessService.findUnixServerInfoByPcid(bean.getPcid());
					UnixServer unixServer = listUnixServer.get(0);
					String[] memoryUsage = unixServer.getMemoryUsage().split("%");
					String memortyUsageOver = bean.getAlramThreshold();
					if (Float.parseFloat(memoryUsage[0]) > Float.parseFloat(memortyUsageOver)) {
						AlarmInfo model = new AlarmInfo();
						model.setAlarmLevel(bean.getAlarmLevel());
						model.setAlarmType(bean.getAlarmType());
						model.setAlarmReason("CPU");
						model.setAlarmSend("系统自动告警");
						model.setAlarmType(bean.getAlarmType());
						model.setMemo("IP：【" + unixServer.getIp() + "】机器的" + "内存使用率【" + memoryUsage[0] + "%】超过阀值【" + bean.getAlramThreshold() + "%】");
						alarmInfoAccessService.save(model);
					}
				} else if (bean.getAlarmType().equals("2")) {// 表空间
					List<DbTableSpace> dbTableSpaceList = dbTableSpaceAccessService.findAllDbTableSpaceInfo(null);
					String tbsOver = bean.getAlramThreshold();
					for (int j = 0; j < dbTableSpaceList.size(); j++) {
						DbTableSpace dbTableSpace = dbTableSpaceList.get(j);
						if (Float.parseFloat(dbTableSpace.getUseAge()) > Float.parseFloat(tbsOver)) {
							AlarmInfo model = new AlarmInfo();
							model.setAlarmLevel(bean.getAlarmLevel());
							model.setAlarmType(bean.getAlarmType());
							model.setAlarmReason("表空间");
							model.setAlarmSend("系统自动告警");
							model.setAlarmType(bean.getAlarmType());
							model.setMemo("数据库服务器表空间使用率【" + dbTableSpace.getUseAge() + "%】" + dbTableSpace.getTableSpaceNmae() + "超过阀值【" + bean.getAlramThreshold() + "%】");
							alarmInfoAccessService.save(model);
						} else
							continue;
					}
				} else if (bean.getAlarmType().equals("3")) {// 磁盘空间
					List<UnixServer> listUnixServer = unixServerAccessService.findUnixServerInfoByPcid(bean.getPcid());
					UnixServer unixServer = listUnixServer.get(0);
					String[] diskUsage = unixServer.getDiskUsage().substring(1, unixServer.getDiskUsage().length() - 1).split(",");
					for (int j = 0; j < diskUsage.length; j++) {
						String temp = diskUsage[j].substring(diskUsage[j].indexOf('=') + 1, diskUsage[j].indexOf('%'));
						if (Float.parseFloat(temp) > Float.parseFloat(bean.getAlramThreshold())) {
							AlarmInfo model = new AlarmInfo();
							model.setAlarmLevel(bean.getAlarmLevel());
							model.setAlarmType(bean.getAlarmType());
							model.setAlarmReason("磁盘空间");
							model.setAlarmSend("系统自动告警");
							model.setAlarmType(bean.getAlarmType());
							model.setMemo("IP：【" + unixServer.getIp() + "】机器的" + "磁盘空间 " + diskUsage[j].substring(0, diskUsage[j].indexOf('=')) + "使用率【" + temp + "%】超过阀值【" + bean.getAlramThreshold()
									+ "%】");
							alarmInfoAccessService.save(model);
						} else
							continue;
					}
				} else
					return;
			}
	}
}
